home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DSIIC2.ARJ / HLP_IO.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  8KB  |  300 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   HLP_IO.C   ***************************/
  4.  
  5. /*
  6. link with:
  7. makehelp.c
  8. hlp_menu.c
  9. mylib.lib
  10. */
  11.  
  12. #include "mydef.h"
  13. #include "help.h"
  14.  
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include  <io.h>
  18. #include <fcntl.h>
  19.  
  20. #if defined TURBOC
  21. /*int putch(int ch);*/
  22. #include <dir.h>
  23. #include <alloc.h>
  24. #include <ctype.h>
  25. #include <conio.h>
  26. #endif
  27.  
  28.  
  29.  
  30.  
  31. #if defined QUICKC
  32.  
  33. #include <sys\types.h>
  34. #include <malloc.h>
  35. #include <string.h>
  36.  
  37. #endif
  38.  
  39. #include <sys\stat.h>
  40.  
  41. /* get_name allows the user to select a file from a "point and shoot"
  42.    menu containing all the file names matching the wild card
  43.    filename */
  44.  
  45.  
  46. void get_name(char *filename)
  47. {
  48. extern struct screen_structure scr;
  49. extern struct window_structure w[];
  50. extern struct hlp help;
  51.  
  52. int done=FALSE;
  53. char ch,ext;
  54. int handle;
  55. int i;
  56. int name_win;  /* window handle for name window */
  57. int dir_win;   /* window handle for directory window */
  58. char temp[80];
  59. char dir_name[80];
  60. char orig_dir[80];
  61. int return_code;
  62. long offset;
  63. int page_size;
  64. int header_size;
  65.  
  66. header_size=sizeof(int)*2;
  67.  
  68. name_win= win_make(1,1,78,4,STD_FRAME,"",scr.normal,scr.normal);
  69.  
  70.  while(!done){
  71.   for(;;){  /* loop to get file name */
  72.     cls();
  73.     print(1,1, "Please name a file (new or existing), or");
  74.     print(1,2,"(Press Enter to select from list.");
  75.     goxy(1,3);
  76.     getcwd(dir_name,79);  /* get the current directory */
  77.     /* if it doesn't have a backslash then add it */
  78.     if(dir_name[strlen(dir_name)-1]!='\\')
  79.        strcat(dir_name,"\\");              /* add it */
  80.     scr.current=scr.inverse;
  81.     ext=getfield(dir_name,78,strlen(dir_name),scr.current);
  82.     trim(dir_name);   /* remove spaces from ends of string */
  83.     scr.current=scr.normal;
  84.      if(ext==ESCAPE){  /* escape */
  85.             strcpy(filename,"");
  86.             win_delete(name_win);
  87.             return;
  88.      }
  89.  
  90.      /* remove any extension from name */
  91.      i=pos(dir_name,".");
  92.      if (i>=0) dir_name[i]='\0';
  93.     /* Did the user enter the name of a path or drive? */
  94.     if(dir_name[strlen(dir_name)-1]=='\\'
  95.        || dir_name[strlen(dir_name)-1]==':'){
  96.     strcpy(orig_dir,dir_name);  /* save the path */
  97.     strcat(dir_name,"*.hlp");
  98.     strcpy(filename,"");
  99.     dir_win= win_make(10,2,13,10,STD_FRAME," Files: ",
  100.                        scr.normal,scr.normal);
  101.     dir(dir_name,filename);
  102.     win_delete(dir_win);
  103.  
  104.     if(strlen(filename)>0){ /* was a file selected? (in temp) */
  105.       strcpy(temp,orig_dir);
  106.       strcat(temp,filename);
  107.       strcpy(filename,temp);
  108.       break;    /* we have a name (break for(;;) loop) */
  109.      }
  110.        else  filename[0]='\0'; /* no file selected from list */
  111.  
  112.     } /* end (is path) */
  113.  
  114.     else{ /* not path, must be name */
  115.      strcpy(filename,dir_name);
  116.      break;
  117.     }
  118.   }   /* end for(;;) loop to get file name*/
  119.  
  120.  /* entry was not a path or directory, let's try to open it */
  121.  /* add the .hlp extension if none was specified */
  122.  
  123.  if(pos(filename,".")==-1)strcat(filename,".hlp");
  124.  
  125.   /* open for append-create if not exist */
  126.  
  127.   handle= open(filename,O_RDWR|O_BINARY);
  128.  
  129.   if (handle==(-1)) { /* create new file ? */
  130.     cls();
  131.     print(1,1,"The file does not exist:");
  132.     print(1,2,"Create a new file y/n ? ");
  133.      for(;;){   /* find out if user wants a new file */
  134.       get_key(&ch,&ext);
  135.       if (toupper(ch)=='Y'){
  136.            return_code=get_size(); /* prompt user for
  137.                                       size of help screen */
  138.            if (return_code==ESCAPE) break;
  139.            handle= open(filename,O_CREAT|O_BINARY|O_RDWR,
  140.                     S_IREAD|S_IWRITE);
  141.  
  142.            if(handle==(-1)){
  143.               cls();
  144.               print(1,1,"Error opening file!");
  145.               print(1,2,"Press any key to continue.");
  146.               get_key(&ch,&ext);
  147.               break;
  148.            }else
  149.            {
  150.             /* write the header */
  151.             write(handle,(char *)&help,header_size);
  152.             close(handle);
  153.             help.number_pages=0; /* new file, no pages yet */
  154.             done=TRUE;
  155.             break;  /* escape the for(;;) loop */
  156.            }
  157.  
  158.       } /* end of if (toupper(ch)=='Y')*/
  159.  
  160.        if (toupper(ch)=='N'){
  161.         putch(ch);
  162.         strcpy(filename,"");
  163.         break;
  164.        }
  165.  
  166.      } /* end for(;;) which asks if user wants to create file */
  167.  
  168.   }   /* end if (handle==(-1))  (create new file?) */
  169.  
  170.  else{ /* file opened (new or existing), now read header */
  171.    /* read the header */
  172.    read(handle,(char *)&help,header_size);
  173.  
  174.    /* calculate page size */
  175.    page_size=sizeof(char)*(help.width*help.height);
  176.  
  177.    /* how many bytes in file? */
  178.    offset=lseek(handle,(long)0,SEEK_END);
  179.  
  180.    /* calculate the number of pages */
  181.    help.number_pages=(offset-(long)header_size)/(long)page_size;
  182.  
  183.    close(handle);
  184.    done=TRUE;
  185.    /*break;*/
  186.  } /* end (read header) */
  187.      print(1,1,"x");
  188.  }  /* end while(!done); */
  189.  
  190.  win_delete(name_win);
  191. }
  192.  
  193.  
  194. int save_page(int page)  /* saves the current help page */
  195. {
  196. extern struct hlp help;
  197. extern struct screen_structure scr;
  198. extern struct window_structure w[];
  199.  
  200. int file_handle;
  201. char *buffer=NULL;
  202. char far *temp;
  203. char far *scrn_ptr;
  204. int i,j;
  205. int page_size;
  206. int header_size;
  207.  
  208.  page_size=sizeof(char)*(help.width*help.height);
  209.  header_size=sizeof(int)*2;
  210.  
  211.  win_pop_top(help.edit);
  212.  scr.current=win_what_attr(help.edit);
  213.  cursor(NORMAL_CURSOR);
  214.  
  215.  /* allocate space, allow for \0 terminator */
  216.  buffer=(char *)malloc ((help.width*help.height)*sizeof(char));
  217.  temp=buffer;    /* set temp pointer = pointer */
  218.  
  219.  for(i=0;i<help.height;i++){  /* get each row */
  220.  
  221.   scrn_ptr=(char far *)(scr.buffer+(scr.top+i-1)*(scr.columns*2)
  222.                +2*(scr.left-1));
  223.     for(j=0;j<help.width;j++){
  224.       *temp=*scrn_ptr;
  225.       temp++;scrn_ptr+=2; /* adjust pointers, skipping attributes */
  226.     }
  227.  }
  228.  
  229.  /* save image to file */
  230.  file_handle=open (help.filename,O_RDWR|O_BINARY);
  231.  lseek(file_handle,(long)(header_size+ page_size*page),SEEK_SET);
  232.  write(file_handle,buffer,page_size);
  233.  close(file_handle);
  234.  
  235.  if(buffer!=NULL)free(buffer);
  236.  win_cls(help.edit); win_redraw_all();win_pop_top(help.menu);
  237.  return(1);
  238. }
  239.  
  240.  
  241. int load_page(int page)   /* load a help page from the file */
  242. {
  243. extern struct hlp help;
  244. extern struct screen_structure scr;
  245. extern struct window_structure w[];
  246.  
  247. int file_handle;
  248. char *buffer;
  249. char *buffer_ptr;
  250. char far *scrn_ptr;
  251. int page_size;
  252. int header_size;
  253. int i,j;
  254.  
  255. page_size=sizeof(char)*(help.width*help.height);
  256. header_size=sizeof(int)*2;
  257.  
  258. buffer=NULL;
  259. buffer=(char*)malloc(page_size);
  260.  
  261.  /* load help page into buffer */
  262.  file_handle=open (help.filename,O_RDWR|O_BINARY);
  263.  lseek(file_handle,(long)(header_size+ page_size*page),SEEK_SET);
  264.  read(file_handle,buffer,page_size);
  265.  close(file_handle);
  266.  
  267.  /* copy buffer to active window */
  268.  
  269.  /* scan the help window for characters */
  270.  
  271.  buffer_ptr=buffer;    /* set temp pointer = buffer pointer */
  272.  for(i=0;i<help.height;i++){  /* get each row */
  273.    scrn_ptr=(char far *)(scr.buffer+(scr.top+i-1)
  274.               *(scr.columns*2)+2*(scr.left-1));
  275.  
  276.    for(j=0;j<help.width;j++){
  277.      *scrn_ptr=*buffer_ptr;    /* copy char from buffer to screen */
  278.      buffer_ptr++;scrn_ptr+=2; /* increment both pointers */
  279.    }
  280.  }
  281.  
  282. if(buffer!=NULL) free(buffer);
  283. return(0);
  284. }
  285.  
  286. /* append a new help page to the file */
  287.  
  288. int append(char *filename, char *buffer, int page_size)
  289. {
  290. int file_handle;
  291.  
  292.  /* write record use file_handle int */
  293.  
  294.  file_handle=open (filename,O_RDWR|O_APPEND|O_BINARY);
  295.  write(file_handle,buffer,page_size);
  296.  close(file_handle);
  297.  
  298.  return (0);
  299. }
  300.